// Copyright 2007-2008 Rory Plaire (codekaizen@gmail.com)
//
// Based initially on JAMA : A Java Matrix Package by 
// National Institute of Standards and Technology (NIST) 
// and The MathWorks, a public-domain work.

using System;
using NPack.Interfaces;

namespace NPack
{
    internal static class Utility
    {
        internal static Double Hypotenuse<T>(T a, T b)
            where T : IEquatable<T>, IComparable<T>, IComputable<T>, IConvertible
        {
            if (a.Abs().GreaterThan(b.Abs()))
            {
                T r = b.Divide(a);
                return Convert.ToDouble(a.Abs().Multiply((r.Multiply(r).Add(b.One)).Sqrt()));
            }

            if (!b.Equals(b.Zero))
            {
                T r = a.Divide(b);
                return Convert.ToDouble(b.Abs().Multiply((r.Multiply(r).Add(b.One)).Sqrt()));
            }

            return 0;
        }

        internal static T Max<T>(T value1, T value2)
            where T : IEquatable<T>, IComparable<T>, IComputable<T>, IConvertible
        {
            if (value1.GreaterThanOrEqualTo(value2))
            {
                return value1;
            }
            else
            {
                return value2;
            }
        }
    }
}